#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "BuyResetCacheData.h"

using boost::property_tree::ptree;

BuyResetCacheData* BuyResetCacheData::mInstance = NULL;

void BuyResetCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new BuyResetCacheData();
	}
}

BuyResetCacheData* BuyResetCacheData::GetInstance()
{
	return mInstance;
}

BuyResetCacheData::BuyResetCacheData()
	: m_maxCount(0)
{
}

BuyResetCacheData::~BuyResetCacheData()
{
	Clear();
}

bool BuyResetCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void BuyResetCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool BuyResetCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/player/buyReset.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/player/buyReset.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}

void BuyResetCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		BuyReset* p = new BuyReset;
		_safe_check(p);

		p->mId = jm["star"].asUInt();

		Json::Value powerBuy = jm["power_buy"];
		uint32_t sz = (uint32_t)(powerBuy.size());
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mPowerBuy.push_back(powerBuy[i].asUInt());
		}

		Json::Value resetStage = jm["reset_stage"];
		sz = (uint32_t)(resetStage.size());
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mResetStage.push_back(resetStage[i].asUInt());
		}

		p->mResetArenaCD = jm["reset_arena_cd"].asUInt();

		Json::Value buyVitality = jm["buy_vitality"];
		sz = (uint32_t)(buyVitality.size());
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mBuyVitality.push_back(buyVitality[i].asUInt());
		}

		Json::Value buySkill = jm["buy_skill"];
		sz = (uint32_t)(buySkill.size());
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mBuySkill.push_back(buySkill[i].asUInt());
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

BuyReset* BuyResetCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool BuyResetCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	BuyReset* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error1");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);


	_safe_guard_end;

	return true;
}

uint32_t BuyResetCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}

